<?php
/**
 * Template Name: 登录模板
 */  
global $wpdb,$user_ID;   
  
if (!$user_ID) { //判断用户是否登录   
	if($_POST){ //数据提交
	   
    //We shall SQL escape all inputs   
    $username = $wpdb->escape($_REQUEST['username']);   
    $password = $wpdb->escape($_REQUEST['password']); 
	$sum = $wpdb->escape($_REQUEST['sum']);    
    $remember = $wpdb->escape($_REQUEST['rememberme']);   
       
    if($remember){   
        $remember = "true";   
    } else {   
        $remember = "false";   
    }   
    $login_data = array();   
    $login_data['user_login'] = $username;   
    $login_data['user_password'] = $password;   
	$login_data['sum'] = $sum;   
    $login_data['remember'] = $remember;   
    $user_verify = wp_signon( $login_data, false );    
    //wp_signon 是wordpress自带的函数，通过用户信息来授权用户(登陆)，可记住用户名   
      
 
 	if ($sum!=$_POST['num1']+$_POST['num2']) {    
        echo "<span class='error'>验证码错误，请重试!</span>";//不管啥错误都输出这个信息   
        exit();    
    } elseif (is_wp_error($user_verify)) { 
		echo "<span class='error'>错误：用户名或密码错误，请重试!</span>";//不管啥错误都输出这个信息      
        exit();      
    }   
	   else{
		echo "<script>window.location.href='/my-account';</script>";  //登陆成功则跳转到首页(ajax提交所以需要用js来跳转)   
		exit();   
	}
	
} 
else {   
  get_header();//载入头部文件   
?>
<section>
<!--s container-->

<div class="container">
  <meta name='robots' content='noindex,nofollow' />
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/login.css" type="text/css" media="all" />
  <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/login.js?v=201303151700"></script>
  <!-- S index Main -->
  <div class="left_main" id="left_side">
    <!--s content_out -->
    <div class="content_out">
      <div class="content">
        <div id="login">
          <div class="registBox">登录
            <div class="loginBoxRel"><a href="<?php bloginfo('url'); ?>/register">注册</a></div>
            
           
            <div class="clear"></div>
          </div>
          <div id="result" align="center"  style="margin-bottom:15px; font-size:14px; color:red"></div>
          <!-- 为ajax返回结果做准备 -->
          <form name="loginform" id="loginform" action="" method="post">
            <p>
              <label for="user_login">用户名：</label>
              <input type="text" name="username" id="user_login" class="input" value="" size="20" tabindex="10" />
            </p>
            <p>
              <label for="user_pass">密码：</label>
              <input type="password" name="password" id="user_pass" class="input" value="" size="20" tabindex="20" />
              </p>
            <?php                
                //获取两个随机数, 范围0~9
                $num1=rand(0,9);
                $num2=rand(0,9);
                //最终网页中的具体内容
                echo"<div class='group' style='margin-bottom:15px'>"
                ."  <label style='float:left;'>验证码：$num1 + $num2 =</label> 
				 <input class='input' type='text' name='sum'  size='10px'  size='20' value='' tabindex='1'> "
                ."<input size='40'  type='hidden' name='num1' value='$num1'>"
                ."<input size='40'  type='hidden' name='num2' value='$num2'>"
                ."<br>"
                ."</div>"; 
                ?>
            <p class="lnn" id='lemail'>
              <label for="email">邮箱：</label>
              <input type="text" name="email" id="email" class="input" value="" size="20" tabindex="30" />
            </p>
            <p class="lnn" id='lnn'>
              <label for="nickname">昵称：</label>
              <input type="text" name="nickname" id="nickname" class="input" value="" size="20" tabindex="30" />
              <span style="clear:both"></span> <span class='alt_message' style="float:right;padding-right:189px">只允许中英文，数字和下划线</span> </p>
            <p class="forgetmenot"> <span style="float:left; width:106px;height:32px; line-height:32px;"></span> </p>
            <p class="submit">
              <input height="30px"  type="checkbox" name="rememberme" value="forever" checked="checked" />
              <span class="alt_message">记住密码</span>
              <input  style="margin-left:30px;" class="button-primary"  id="submitbtn" type="submit" name="user-submit" value="登录" />
              <input type="hidden" name="user-cookie" value="1" />
            </p>
            
            
            <div class="clear"></div>
          </form> 
          <div class="loginBoxRel" style=" width:75px"><a href="<?php bloginfo('url'); ?>/reset" target="_blank" title="找回密码">忘记密码？</a></div>
        </div>
        <div class="clear"></div>
      </div>
    </div>
  </div>
  <script type="text/javascript"><!--ajax 提交数据-->                        
$("#submitbtn").click(function() {     
var input_data = $('#loginform').serialize();   
$.ajax({   
type: "POST",   
url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",   
data: input_data,   
success: function(msg){   
$('.loader').remove();   
$('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');   
}   
});   
return false;   
  
});   
</script>
  <!-- E Main -->
  <?php get_sidebar();?>
  <div class="clear"></div>
</div>
<?php   
get_footer(); //载入底部文件         
}   
}else { //跳转到首页   
    echo "<script type='text/javascript'>window.location='". get_bloginfo('url')."'</script>";   
}   
?>
